package com.admin.dao;

import com.admin.bean.Advert;
import com.admin.bean.Flight;
import com.admin.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class advertDao {
    public List<Advert> selectadvert() {
        List<Advert>ad=new ArrayList<Advert>();
        try {
            Connection connection= JDBCUtils.getConnection();
            String sql="select * from `advert` ";
            PreparedStatement ps=connection.prepareStatement(sql);
            ResultSet resultSet =ps.executeQuery();
            while (resultSet.next()){
                Advert advert=new Advert();
                advert.setAdvertId(resultSet.getString(1));
                advert.setAdvertDescribe(resultSet.getString(2));
                advert.setUrl(resultSet.getString(3));
                advert.setPicture(resultSet.getString(4));
                ad.add(advert);
            }
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return ad;
    }


    public boolean addAdvert(Advert advert) {
        try {
            Connection connection = JDBCUtils.getConnection();
            String sql = "INSERT INTO `advert` VALUES (?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,advert.getAdvertId());
            ps.setString(2,advert.getAdvertDescribe());
            ps.setString(3,advert.getUrl());
            ps.setString(4,advert.getPicture());
            System.out.println(ps);
            ps.execute();

        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean deleteAdvert(String advertId) {
        try {
            Connection connection= JDBCUtils.getConnection();
//            flightId="'"+flightId+"'";
            String sql="delete from `advert` where advertId in ("+advertId+")";
            Statement st=connection.createStatement();
//            System.out.println(advertId+"\n"+sql);
            st.executeUpdate(sql);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return true;
    }
}
